package com.lhd.weibo.controller;

import com.lhd.weibo.pojo.dto.WeiboDTO;
import com.lhd.weibo.pojo.entity.Weibo;
import com.lhd.weibo.pojo.vo.WeiboDetailVO;
import com.lhd.weibo.pojo.vo.WeiboIndexVO;
import com.lhd.weibo.response.JsonResult;
import com.lhd.weibo.response.StatusCode;
import com.lhd.weibo.service.WeiboService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lhd
 * date 2024/5/16
 * @apiNote
 */
@Slf4j
@RestController
@RequestMapping("/v1/weibos")
public class WeiboController {

    @Autowired
    private WeiboService weiboService;

    /**
     * 添加新的微博记录
     */
    @PostMapping("add-new")
    public JsonResult addNew(WeiboDTO weiboDTO){
        log.info("传入的参数: ", weiboDTO);

        int result = weiboService.addWeibo(weiboDTO);
        log.info("插入微博表后返回的插入条数: ", result);
        if (result > 0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }

    }

    /**
     * 查询所有的微博记录
     */
    @GetMapping("list")
    public JsonResult listForIndex(){

        List<WeiboIndexVO> weiboList = weiboService.listForIndex();

        return new JsonResult(StatusCode.OPERATION_SUCCESS, weiboList);
    }

    /**
     * 查询指定用户的微博
     */
    @GetMapping("")
    public JsonResult listForIndexByUserId(Long userId){
        log.info("传入的参数: {}", userId);
        List<WeiboIndexVO> weiboList = weiboService.listForIndexByUserId(userId);
        return JsonResult.ok(weiboList);
    }

    /**
     * 根据id删除微博记录
     */
    @PostMapping("{id}/delete")
    public JsonResult deleteById(@PathVariable Long id){
        log.info("传入的参数: {}", id);
        weiboService.deleteById(id);
        return JsonResult.ok();
    }

    /**
     * 根据微博id查询微博详情
     */
    @GetMapping("{id}/detail")
    public JsonResult detail(@PathVariable Long id){
        WeiboDetailVO weiboDetailVO = weiboService.detailById(id);
        return JsonResult.ok(weiboDetailVO);
    }

}
